package com.example.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;


@Entity
@Table(name="t_user")
public class User {


    public User() {
        super();
        this.banks = new ArrayList<>();
        this.dept = new ArrayList<>();
    }
    @Id
    private String uid;
    private String name;

    @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER, orphanRemoval=true)
    @JoinColumn(name="address_id", referencedColumnName="aid")
    private Address address;

    @ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    @JoinTable(name="user_dept", joinColumns=@JoinColumn(name="user_id", referencedColumnName="uid"),
        inverseJoinColumns=@JoinColumn(name="dept_id", referencedColumnName="did"))
    private List<Department> dept;

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, orphanRemoval=true)
    @JoinColumn(name="user_id", referencedColumnName="uid")
    private List<Bank> banks;


    public String getUid() {
        return uid;
    }
    public void setUid(String uid) {
        this.uid = uid;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    public List<Department> getDept() {
        return dept;
    }
    public void setDept(List<Department> dept) {
        this.dept.clear();
        this.dept.addAll(dept);
    }
    public List<Bank> getBanks() {
        return banks;
    }
    public void setBanks(List<Bank> banks) {
        this.banks.clear();
        this.banks.addAll(banks);
    }

    @Override
    public String toString() {
        return String.format("User [uid=%s, name=%s, address=%s, dept=%s, banks=%s]", uid, name, address, dept, banks);
    }


}
